import logging
import time

from appium.webdriver.common.mobileby import MobileBy
from Hogwarts.Homeworks.hw0704.page_objects.base import Base
from Hogwarts.Homeworks.hw0704.page_objects.page_member_profile import PageMemberProfile

"""
PAGE：通讯录页面
"""


class PageContact(Base):

    # OBJECTS
    _ADD_MEMBER = (MobileBy.XPATH, "//*[@text='添加成员']")
    _ITEMS_MEMBER = (MobileBy.XPATH, "//*[@text='企业通讯录']"
                                     "/.."
                                     "/following-sibling::"
                                     "*[@class='android.widget.RelativeLayout']"
                                     "//*[@class='android.widget.TextView']")

    def click_add_member(self):
        """
        点击"添加成员"
        :return:
        """
        from Hogwarts.Homeworks.hw0704.page_objects.page_contact_add_choose import PageContactAddChoose
        logging.info("STEP：点击添加成员")

        self.find_and_click(*self._ADD_MEMBER)
        return PageContactAddChoose(self.driver)

    def tap_member_item(self, member_name):
        """
        点击成员条目
        :return:
        """

        logging.info("STEP：点击成员条目")
        self.find_with_scroll(member_name).click()

        return PageMemberProfile(self.driver)

    def get_member_names(self):
        """
        获取通讯录成员名字，方便后续断言使用
        :return:
        """
        time.sleep(2)

        member_names = []
        elements = self.finds(*self._ITEMS_MEMBER)
        for ele in elements:
            member_names.append(ele.text)

        return [name for name in member_names if name != '添加成员']
